Imports IWshRuntimeLibrary

Public Class frmMain
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        dlgOpen.RootFolder = Environment.SpecialFolder.ProgramFiles
        dlgOpen.ShowDialog()
        If MsgBox("Create new subdirectory '" & dlgOpen.SelectedPath & "\ERN'?", MsgBoxStyle.YesNo + MsgBoxStyle.DefaultButton1) = MsgBoxResult.Yes Then
            txtLocation.Text = dlgOpen.SelectedPath & "\ERN"
        Else
            txtLocation.Text = dlgOpen.SelectedPath
        End If
    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Application.Exit()
    End Sub

    Private Sub btnInstall_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInstall.Click
        Dim wsh As New IWshRuntimeLibrary.WshShell
        Dim shortcut As IWshRuntimeLibrary.IWshShortcut
        Dim procInfo As New Diagnostics.ProcessStartInfo
        Dim uninstallFile As IO.TextWriter
        Dim unFiles() As String
        Dim unRegs() As String
        Dim unFile As Integer = 0
        Dim unReg As Integer = 0
        Dim i As Integer
        Dim fileCopyOK As Boolean = True

        Const fileCount As Integer = 7
        Const regCount As Integer = 0
        Const dirCount As Integer = 2
        Const shortcuts As Integer = 3

        btnExit.Enabled = False
        btnInstall.Enabled = False

        ReDim unFiles(fileCount + shortcuts)
        ReDim unRegs(regCount)

        'add one at the end for doing the uninstall info
        'add one at the end for the add remove programs info in the registry!
        progress.Step = Math.Ceiling(100 / (dirCount + fileCount + regCount + shortcuts + 1 + 1))

        'create directory
        lblStatus.Text = "Creating directories..."
        IO.Directory.CreateDirectory(txtLocation.Text)
        progress.PerformStep()
        IO.Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.StartMenu) & "\ERN")
        progress.PerformStep()

        'copy files
        lblStatus.Text = "Copying files..."
        Try
            IO.File.Copy(Application.StartupPath & "\ERN_Admin.exe", txtLocation.Text & "\ERN_Admin.exe", True)
        Catch
            fileCopyOK = False
        End Try
        unFiles(unFile) = txtLocation.Text & "\ERN_Admin.exe"
        unFile = unFile + 1
        progress.PerformStep()
        Try
            IO.File.Copy(Application.StartupPath & "\auEngine.exe", txtLocation.Text & "\auEngine.exe", True)
        Catch
            fileCopyOK = False
        End Try
        unFiles(unFile) = txtLocation.Text & "\auEngine.exe"
        unFile = unFile + 1
        progress.PerformStep()
        Try
            IO.File.Copy(Application.StartupPath & "\auGenerator.exe", txtLocation.Text & "\auGenerator.exe", True)
        Catch
            fileCopyOK = False
        End Try
        unFiles(unFile) = txtLocation.Text & "\auGenerator.exe"
        unFile = unFile + 1
        progress.PerformStep()
        Try
            IO.File.Copy(Application.StartupPath & "\ICSharpCode.SharpZipLib.dll", txtLocation.Text & "\ICSharpCode.SharpZipLib.dll", True)
        Catch
            fileCopyOK = False
        End Try
        unFiles(unFile) = txtLocation.Text & "\ICSharpCode.SharpZipLib.dll"
        unFile = unFile + 1
        progress.PerformStep()
        Try
            IO.File.Copy(Application.StartupPath & "\ZipHelper.dll", txtLocation.Text & "\ZipHelper.dll", True)
        Catch
            fileCopyOK = False
        End Try
        unFiles(unFile) = txtLocation.Text & "\ZipHelper.dll"
        unFile = unFile + 1
        progress.PerformStep()
        Try
            IO.File.Copy(Application.StartupPath & "\ErrorHandling.dll", txtLocation.Text & "\ErrorHandling.dll", True)
        Catch
            fileCopyOK = False
        End Try
        unFiles(unFile) = txtLocation.Text & "\ErrorHandling.dll"
        unFile = unFile + 1
        progress.PerformStep()
        Try
            IO.File.Copy(Application.StartupPath & "\euu.exe", txtLocation.Text & "\euu.exe", True)
        Catch
            fileCopyOK = False
        End Try
        progress.PerformStep()

        'create shortcuts
        lblStatus.Text = "Creating shortcuts..."
        shortcut = CType(wsh.CreateShortcut(Environment.GetFolderPath(Environment.SpecialFolder.StartMenu) & "\ERN\ERN Admin.lnk"), IWshRuntimeLibrary.IWshShortcut)
        With shortcut
            .TargetPath = txtLocation.Text & "\ern_admin.exe"
            .WorkingDirectory = txtLocation.Text
            .WindowStyle = 1
            .Description = "ERN Admin"
            .IconLocation = .TargetPath
            .Save()
        End With
        unFiles(unFile) = Environment.GetFolderPath(Environment.SpecialFolder.StartMenu) & "\ERN\ERN Admin.lnk"
        unFile = unFile + 1
        progress.PerformStep()
        shortcut = CType(wsh.CreateShortcut(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\Microsoft\Internet Explorer\Quick Launch\ERN Admin.lnk"), IWshRuntimeLibrary.IWshShortcut)
        With shortcut
            .TargetPath = txtLocation.Text & "\ern_admin.exe"
            .WorkingDirectory = txtLocation.Text
            .WindowStyle = 1
            .Description = "ERN Admin"
            .IconLocation = .TargetPath
            .Save()
        End With
        unFiles(unFile) = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\Microsoft\Internet Explorer\Quick Launch\ERN Admin.lnk"
        unFile = unFile + 1
        progress.PerformStep()
        shortcut = CType(wsh.CreateShortcut(Environment.GetFolderPath(Environment.SpecialFolder.StartMenu) & "\ERN\auGenerator.lnk"), IWshRuntimeLibrary.IWshShortcut)
        With shortcut
            .TargetPath = txtLocation.Text & "\auGenerator.exe"
            .WorkingDirectory = txtLocation.Text
            .WindowStyle = 1
            .Description = "AutoUpdate Generator"
            .IconLocation = .TargetPath
            .Save()
        End With
        unFiles(unFile) = Environment.GetFolderPath(Environment.SpecialFolder.StartMenu) & "\ERN\auGenerator.lnk"
        unFile = unFile + 1
        progress.PerformStep()

        'build uninstall info file
        uninstallFile = IO.File.CreateText(txtLocation.Text & "\uninstallAdmin.log")
        uninstallFile.WriteLine("files:" & (unFiles.Length - 2))
        uninstallFile.WriteLine("registries:" & (unRegs.Length - 1))
        For i = 0 To unFiles.Length - 1
            If unFiles(i) <> "" Then uninstallFile.WriteLine(unFiles(i))
        Next
        For i = 0 To unRegs.Length - 1
            If unRegs(i) <> "" Then uninstallFile.WriteLine(unRegs(i))
        Next
        uninstallFile.Close()
        progress.PerformStep()

        'create the add/remove programs info
        Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall", True).CreateSubKey("ERN Admin")
        Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\ERN Admin", True).SetValue("DisplayName", "ERN Admin")
        Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\ERN Admin", True).SetValue("DisplayVersion", "")
        Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\ERN Admin", True).SetValue("HelpLink", "http://www.noworriescomputerhelp.com/ern")
        Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\ERN Admin", True).SetValue("NoModify", "1", Microsoft.Win32.RegistryValueKind.DWord)
        Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\ERN Admin", True).SetValue("NoRepair", "1", Microsoft.Win32.RegistryValueKind.DWord)
        Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\ERN Admin", True).SetValue("Publisher", "Dan Cleveland")
        Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\ERN Admin", True).SetValue("UninstallString", ControlChars.Quote & txtLocation.Text & "\euu.exe" & ControlChars.Quote & " uninstallAdmin.log")
        Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\ERN Admin", True).SetValue("URLInfoAbout", "http://www.noworriescomputerhelp.com/ern")
        Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\ERN Admin", True).SetValue("URLUpdateInfo", "http://www.noworriescomputerhelp.com/ern")
        Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\ERN Admin", True).SetValue("DisplayIcon", ControlChars.Quote & txtLocation.Text & "\ern_admin.exe" & ControlChars.Quote)
        progress.PerformStep()

        'execute ern admin client
        With procInfo
            .FileName = txtLocation.Text & "\ern_admin.exe"
            .WorkingDirectory = txtLocation.Text
        End With
        Process.Start(procInfo)

        If Not fileCopyOK Then MsgBox("Some destination files are in use and could not be copied", MsgBoxStyle.OkOnly, "ERN Admin Setup")
        MsgBox("Installation complete!")
        Application.Exit()
    End Sub

    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        txtLocation.Text = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) & "\ERN"

        If IO.File.Exists(Application.StartupPath & "\license.txt") Then
            If frmLicense.ShowDialog() = Windows.Forms.DialogResult.Cancel Then
                MsgBox("You must accept the license agreement if you wish to use this program.  Please rerun this setup program if you decide to accept the agreement.")
                Application.Exit()
            End If
        End If
    End Sub
End Class
